<?php

namespace app\admin\controller;

use think\Db;
use app\admin\model\Banner as Banimg;
use think\Request;
use think\Validate;

class Banner extends Main
{
   private $db;

   public function  __construct() {
       parent::__construct();
       $this->db = Db::name('banner');
   }

   public function index()
   {
        $this->assign('item',['item1'=>'网站管理','item2'=>'轮播图列表']);
        return  $this->fetch();
   }

   public function get_table_data()
   {
       //获取分页page和limit参数
       $page=input("get.page")?input("get.page"):1;
       $page=intval($page);
       $limit=input("get.limit")?input("get.limit"):10;
       $limit=intval($limit);
       $start=$limit*($page-1);

       $count = $this->db->count("id");
       $data_list = $this->db->limit($start,$limit)->order('id desc')->select();
       $list["msg"]="";
       $list["code"]=0; //状态码
       $list["count"]=$count;
       $list["data"]=$data_list;
       if(empty($data_list)){
           $list["msg"]="暂无数据";
       }
       return json($list);
   }

   public function add_banner()
   {
    if(Request::instance()->isPost()){//is post
		$post = $this->request->post();
		$validate = validate('Banner');
		$res = $validate->check($post);
		if($res !== true){//没有通过验证
		   $this->error($validate->getError());
		}else{//通过验证
			$data = ['url'=>$post['url'],'banner'=>$post['bennerImg'],
					'describe'=>$post['describe'],'flg'=>$post['flg'],
					'status' => $post['status'],'datetime'=>date('Y-m-d H:i:s',time())];
			$this->db->insert($data);
            setLog('增加轮播图','Banner/add_banner');
			$this->success('success');
		}
    }else{
        return  $this->fetch();
    }
   }
   
   public function edit_banner()
   {
	   $id = $this->request->get('id');
	   if($id){
		   $res = $this->db->where('id',$id)->find();
		   $this->assign('data',$res);
		   return  $this->fetch();
	   }else{
		   $this->error('非法操作');
	   }
   }
   
   public function deleteRow()
   {
	   $id = $this->request->get('id');
	   if($id){
		   $res = $this->db->where('id',$id)->delete();
		   if($res){
		       setLog('删除轮播图','Banner/add_banner');
			$this->success('success');
		   }else{
			   $this->error('删除失败');
		   }
	   }else{
		   $this->error('非法操作');
	   }  
   }
   
   public function update_banner(){
	   if(Request::instance()->isPost()){//is post
			$post = $this->request->post();
			$validate = validate('Banner');
			$res = $validate->check($post);
			if($res !== true){//没有通过验证
			   $this->error($validate->getError());
			}else{//通过验证
				$data = ['url'=>$post['url'],'banner'=>$post['bennerImg'],
						'describe'=>$post['describe'],'flg'=>$post['flg'],
						'status' => $post['status'],'datetime'=>date('Y-m-d H:i:s',time())];
                $this->db->where(['id' => $post['id']])->update($data);
				$param = ['name'=>'修改轮播图','way'=>'Banner/add_banner','username'=>session('username'),'create_time'=>date('Y-m-d H:i:s',time())];
					\think\Hook::listen('log',$param);
				$this->success('success');
			}
	   }else{
		   $this->error('非法操作');
	   }
   }

   public function bachDel(){
       if(Request::instance()->isPost()){//is post
           $post = $this->request->post();
           $id = implode(',',$post['id']);
           $this->db->whereIn('id',$id)->delete();
           $param = ['name'=>'批量删除轮播图','way'=>'Banner/bachDel','username'=>session('username'),'create_time'=>date('Y-m-d H:i:s',time())];
           \think\Hook::listen('log',$param);
           $this->success('success');
       }else{
           $this->error('非法操作');
       }
   }
   
   public function upload()
   {
	   $file = request()->file('img');
		// 移动到框架应用根目录/public/uploads/ 目录下
		$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'Banner');
		if($info){
		$name =  $info->getSaveName();
		$name =  str_replace('\\' ,'/', $name);
		$res = array("code"=>0,"msg"=>"","data"=>array('src'=>'Banner/' . $name));
		echo json_encode($res);
		}else{
		$res = array("code"=>1,"msg"=>"","data"=>[]);
		// 上传失败获取错误信息
		echo $file->getError();
		} 
   }
   
//class 结束
}